package com.doumuxie.dto;

import com.doumuxie.frame.ChildFrame;
import com.doumuxie.handler.ProcessHandler;
import com.doumuxie.util.SysCode;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/9/26 9:03
 * @description 线程数据结构实体
 **/
public class ProcessDto implements Comparable<ProcessDto> {

    /**
     * 进程唯一id
     */
    private String id;

    /**
     * 进程名称
     */
    private String name;

    /**
     * 优先级
     */
    private int level;

    /**
     * 当前进程状态
     * 1 就绪
     * 2 阻塞
     * 3 执行
     * 4 消亡
     */
    private String state;

    /**
     * 时间片数量
     */
    private int timeSliceCount;

    /**
     * 数据
     */
    private int data = 0;

    /**
     * 是否在运行
     */
    private boolean isRun;

    private int lessTime = 5;

    public int getLessTime() {
        return lessTime;
    }

    public void setLessTime(int lessTime) {
        this.lessTime = lessTime;
    }

    public boolean isRun() {
        return isRun;
    }

    public void setRun(boolean run) {
        isRun = run;
    }

    /**
     * 当前进程对应的线程 模拟进程控制
     */
    private Thread thread;

    @Override
    public String toString() {
        return "ProcessDto{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", level=" + level +
                ", state='" + state + '\'' +
                ", timeSliceCount=" + timeSliceCount +
                ", data=" + data +
                ", isRun=" + isRun +
                '}';
    }


    /**
     * 重写比较方法  用于排序
     *
     * @param dto
     * @return
     */
    @Override
    public int compareTo(ProcessDto dto) {
        // 降序
        return dto.getLevel() - this.getLevel();

    }


    /**
     * 构造方法
     *
     * @param frame
     */
    public ProcessDto(ChildFrame frame) {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.err.println("开始执行run方法");
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (isRun) {

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            isRun = false;
                        }
                        // 阻塞检测
                        if (SysCode.PROCESS_STATE.BLOCK.equals(state)) {
                            continue;
                        }
                        // 运行中的非当前进程检测
                        if (!id.equals(ProcessHandler.runningProcessId)) {
                            continue;
                        }
                        data++;
                        lessTime--;
                        if (lessTime < 1) {
                            isRun = false;
                            lessTime = 5;
                        }
                        System.err.println("进程：" + id + "，数据：" + data);
                        ProcessHandler.updateRowData(id, data, frame);
                        // 时间片运行结束
                        if (!isRun) {
                            // 时间片-1
                            timeSliceCount--;
                            // 优先级 -1
                            if (level > 1) {
                                level--;
                            }
                            if (timeSliceCount <= 0) {
                                // 进入消亡队列
                                state = SysCode.PROCESS_STATE.DEATH;
                            } else {
                                // 设置为就绪状态
                                state = SysCode.PROCESS_STATE.NEW;
                            }
                            ProcessHandler.sort();
                            // 执行优先级下一个
                            ProcessHandler.nextProcess(null);
                            ProcessHandler.initRowData(frame);
                            break;
                        }
                    }

                    if (SysCode.PROCESS_STATE.DEATH.equals(state)) {
                        System.err.println("线程消亡：" + thread.getState());
                        return;
                    }
                }
            }
        });
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getTimeSliceCount() {
        return timeSliceCount;
    }

    public void setTimeSliceCount(int timeSliceCount) {
        this.timeSliceCount = timeSliceCount;
    }
}
